#ifndef __EMODBUS_H
#define __EMODBUS_H

#include <stdio.h>

#include "emodbus_crc.h"

#define E_MODBUS_RX_BUFF_SIZE    128
#define E_MODBUS_TX_BUFF_SIZE    256

#define E_MODBUS_R_REG_SIZE    16
#define E_MODBUS_RW_REG_SIZE    32

//读功能码
#define E_MODBUS_READ_FUNC_CODE    0x03
//私有协议内容
#define E_MODBUS_PRAViTE_FUNC_CODE    0x02
//写功能码
#define E_MODBUS_WRITE_FUNC_CODE    0x06


/*
 * salve id
 * 地址表定义
 * 只读寄存器
 * 读写寄存器
 */
typedef struct {
    uint8_t  slave_id;

    uint16_t  read_reg_start_addr;
    uint16_t  read_reg_length;
    uint16_t  *read_reg_p;

    uint16_t  read_write_reg_start_addr;
    uint16_t  read_write_reg_length;
    uint16_t  *read_write_reg_p;
}_EModbusDec;

typedef struct {
    uint8_t  slave_id;
    uint8_t  func_code;

    //读写数据定义
    uint16_t  read_write_reg_start;
    uint16_t  read_write_reg_length;
    uint8_t  *read_write_data_p;

    //私有功能码
    uint8_t  func_private_code1;
    uint8_t  func_private_code2;

    uint16_t  crc;

    uint16_t data_length;
}_EModbusDataDec;

uint8_t exec_emodbus(uint8_t *data_buff);


uint8_t  verify_emodbus_rx_data(uint8_t *data_buff);
void show_emodbus_verify_data();
void pop_top_index_clear();
void push_last_index_clear();
uint8_t pop_top(uint8_t *data_buff);
uint8_t push_last(uint8_t *data_buff, uint8_t data);
void emodbus_dec_clear();
void show_r_w_reg();
uint8_t verify_emodbus_rw_addr();
void show_emodbus_tx_data(uint16_t length);
void clear_tx_data();


#endif